package com.atguigu.tingshu;

import com.atguigu.tingshu.common.constant.RedisConstant;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableScheduling//开启定时任务
@Slf4j
public class ServiceAlbumApplication implements CommandLineRunner {

    //private static final Logger log = LoggerFactory.getLogger(ServiceAlbumApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ServiceAlbumApplication.class, args);
    }

    //初始化布隆过滤器
    @Autowired
    private RedissonClient redissonClient;
    @PostConstruct
    public void initBloomFilter() {
        log.info("模拟初始化布隆过滤器开始...");
    }


    /**
     * springboot启动后自动执行一次该方法
     * 项目启动后自动初始化布隆过滤器
     */
    @Override
    public void run(String... args) throws Exception {
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        log.info("布隆过滤器是否存在：{}", bloomFilter.isExists());
        if (!bloomFilter.isExists()){//不存在，初始化
            boolean flag = bloomFilter.tryInit(1000000, 0.01);
            log.info("初始化布隆过滤器，完成结果：{}", flag);
        }
    }
}
